﻿using Bcmm.Chart.Common;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace Bcmm.Chart.Controls
{
    public class Waveform : UIElement
    {
        public ObservableCollection<Point> Points { get; set; }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Line line = new Line();
            line.Pen = new Pen(Brushes.Red, 1);

            int count = Points.Count % 2 == 0 ? Points.Count : Points.Count - 1;

            for (int k = 1; k < count; k++)
            {
                line.StartPoint = Points[k - 1];
                line.EndPoint = Points[k];
                line.Render(drawingContext);
            }
        }

        public Waveform()
        {
            SnapsToDevicePixels = true;

            Random random = new Random();
            Points = new ObservableCollection<Point>();

            for (int k = 0; k < 100; k++)
            {
                var point = new Point(k, random.Next(1, 100));
                Points.Add(point);
            }

            using SQLiteHelper sqliteHelper = new SQLiteHelper();
            var conn = sqliteHelper.CreateTempLocalConnection();
            conn.Open();
            var command = conn.CreateCommand();

            command.CommandText = "CREATE TABLE TEMPTABLE (FirtColumn double, SecondColumn double);";
            command.ExecuteNonQuery();

            command.CommandText = "INSERT INTO TEMPTABLE VALUES (100, 100);";
            command.ExecuteNonQuery();

            command.CommandText = "SELECT * FROM TEMPTABLE;";
            var reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    double column1 = reader.GetDouble(1);
                }
            }

            reader.Close();
            command.Dispose();

            conn.Close();
        }
    }
}
